Shader "Water/flowmapShader"
{
    Properties
    {
        _MainTex ("Texture", 2D) = "white" {}
        _FlowMap("FlowMap", 2D) = "white" {}
        _TimeSpeed("globalSpeed", float) = 1
    }
    SubShader
    {
        Tags { "RenderType"="Opaque" }
        LOD 100

        Pass
        {
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag
            // make fog work
            #pragma multi_compile_fog

            #include "UnityCG.cginc"

            struct appdata
            {
                float4 vertex : POSITION;
                float2 uv : TEXCOORD0;
            };

            struct v2f
            {
                float2 uv : TEXCOORD0;
                UNITY_FOG_COORDS(1)
                float4 vertex : SV_POSITION;
            };

            sampler2D _MainTex;
            sampler2D _FlowMap;
            float4 _MainTex_ST;

            v2f vert (appdata v)
            {
                v2f o;
                o.vertex = UnityObjectToClipPos(v.vertex);
                o.uv = TRANSFORM_TEX(v.uv, _MainTex);
                //UNITY_TRANSFER_FOG(o,o.vertex);
                return o;
            }

            fixed4 frag (v2f i) : SV_Target
            {

                float3 flowDir = tex2D(_FlowMap, i.uv) * 2.0f - 1.0f;

                float phase0 = frac(_Time.y + 0.5) ;
                float phase1 = frac(_Time.y) ;

                float4 color0 = tex2D(_MainTex, i.uv + phase0 * flowDir.xy) ;
                float4 color1 = tex2D(_MainTex, i.uv + phase1 * flowDir.xy);

                float flowLerp = (abs(phase0 - 0.5)) * 2;
                float4 finalCol = lerp(color0, color1, flowLerp);

                return finalCol;
            }
            ENDCG
        }
    }
}
